home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Prog / D-G / DropInfo ƒ / DI⁄C / DSAppleEvents.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-24  |  7.8 KB  |  249 lines  |  [TEXT/MPS ]

  1. /******************************************************************************
  2. **
  3. **  Project Name:    DropShell
  4. **     File Name:    DSAppleEvents.c
  5. **
  6. **   Description:    Generic AppleEvent handling routines
  7. **                    
  8. **                    This is the set of routines for handling the required Apple events.
  9. **                    You should NEVER have to modify this file!!!
  10. **                    Simply add code in DSUserProcs to the routines called by these.
  11. **
  12. *******************************************************************************
  13. **                       A U T H O R   I D E N T I T Y
  14. *******************************************************************************
  15. **
  16. **    Initials    Name
  17. **    --------    -----------------------------------------------
  18. **    LDR            Leonard Rosenthol
  19. **    MTC            Marshall Clow
  20. **    SCS            Stephan Somogyi
  21. **
  22. *******************************************************************************
  23. **                      R E V I S I O N   H I S T O R Y
  24. *******************************************************************************
  25. **
  26. **      Date        Time    Author    Description
  27. **    --------    -----    ------    ---------------------------------------------
  28. **    11/24/91            LDR        Added a handler for 'pdoc' as per DTS recommendation
  29. **                                    This caused some reorg & userProc routine changes
  30. **                                    I also created a new common AEVT doc extractor
  31. **                                Cleaned up error handling by adding FailErr
  32. **                                Cleaned up the placement of braces
  33. **                                Added the passing of a userDataHandle to the odoc/pdoc routines
  34. **    10/29/91            SCS        Changes for THINK C 5
  35. **    10/28/91            LDR        Officially renamed DropShell (from QuickShell)
  36. **                                Added a bunch of comments for clarification
  37. **    10/06/91    00:02    MTC        Converted to MPW C
  38. **    04/09/91    00:02    LDR        Added to Projector
  39. **
  40. ******************************************************************************/
  41.  
  42. #include "DSGlobals.h"
  43. #include "DSUserProcs.h"
  44. #include "DSAppleEvents.h"
  45.  
  46.  
  47. /*
  48.     This routine does all initialization for AEM, including the
  49.     creation and then population of the dispatch table.
  50. */
  51. #pragma segment Initialize
  52. pascal void InitAEVTStuff ()  {
  53.     OSErr aevtErr;
  54.  
  55.     aevtErr = noErr;
  56.     
  57.     if ( aevtErr == noErr )
  58.         aevtErr = AEInstallEventHandler ( kCoreEventClass, kAEOpenApplication,
  59.                      (EventHandlerProcPtr) HandleOAPP, 0, false );
  60.  
  61.     if ( aevtErr == noErr )
  62.         aevtErr = AEInstallEventHandler ( kCoreEventClass, kAEOpenDocuments,
  63.                     (EventHandlerProcPtr) HandleODOC, 0, false );
  64.  
  65.     if ( aevtErr == noErr )
  66.         aevtErr = AEInstallEventHandler ( kCoreEventClass, kAEPrintDocuments,
  67.                     (EventHandlerProcPtr) HandlePDOC, 0, false );
  68.  
  69.     if ( aevtErr == noErr )
  70.         aevtErr = AEInstallEventHandler ( kCoreEventClass, kAEQuitApplication,
  71.                     (EventHandlerProcPtr) HandleQuit, 0, false );
  72.  
  73.     if ( aevtErr == noErr )
  74.         InstallOtherEvents ();
  75.         
  76.     if ( aevtErr != noErr )
  77.         ;        // report an error if you are so included
  78. }
  79.  
  80.  
  81.  
  82. /*    
  83.     This routine is a utility routine for checking that all required 
  84.     parameters in the Apple event have been used.
  85. */
  86. #pragma segment AEVT
  87. OSErr GotRequiredParams ( AppleEvent *theAppleEvent ) {
  88.     DescType    typeCode;
  89.     Size        actualSize;
  90.     OSErr        retErr, err;
  91.  
  92.     err = AEGetAttributePtr ( theAppleEvent, keyMissedKeywordAttr,
  93.                     typeWildCard, &typeCode, NULL, 0, &actualSize );
  94.     
  95.     if ( err == errAEDescNotFound )    // we got all the required params: all is ok
  96.         retErr = noErr;
  97.     else if ( err == noErr )
  98.         retErr = errAEEventNotHandled;
  99.     else 
  100.         retErr = err;
  101.     
  102.     return retErr;
  103. }
  104.  
  105. /*
  106.     This is another routine useful for showing debugging info.
  107.     It calls the ErrorAlert routine from DSUtils to put up the 
  108.     error message.
  109.  
  110. */
  111. void FailErr(OSErr err) {
  112.  
  113.     if (err != noErr)
  114.         ErrorAlert(kErrStringID, kAEVTErr, err);
  115. }
  116.  
  117. /*    
  118.     This routine is the handler for the oapp (Open Application) event.
  119.     
  120.     It first checks the number of parameters to make sure we got them all 
  121.     (even though we don't want any) and then calls the OpenApp userProc in QSUserProcs.
  122.     Finally it checks to see if the caller wanted a reply & sends one, setting any error.
  123. */
  124. #pragma segment AEVT
  125. pascal OSErr HandleOAPP ( AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefcon ) {
  126. #pragma unused ( handlerRefcon )
  127.     OSErr err;
  128.  
  129.     FailErr(err = GotRequiredParams ( theAppleEvent ));
  130.  
  131.     OpenApp ();        // pass it on to the app specific routine
  132.  
  133.     if ( reply->dataHandle != NULL )    /*    a reply is sought */
  134.         FailErr(err = AEPutParamPtr ( reply, 'errs', 'TEXT', "Opening", 7 ));
  135.     
  136.     return err;
  137. }
  138.  
  139.  
  140. /*    
  141.     This routine is the handler for the quit (Quit Application) event.
  142.     
  143.     It first checks the number of parameters to make sure we got them all 
  144.     (even though we don't want any) and then calls the QuitApp userProc in QSUserProcs.
  145.     Finally it checks to see if the caller wanted a reply & sends one, setting any error.
  146. */
  147.  
  148. #pragma segment AEVT
  149. pascal OSErr HandleQuit ( AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefcon ) {
  150. #pragma unused ( handlerRefcon )
  151.     OSErr err;
  152.     
  153.     FailErr( err = GotRequiredParams ( theAppleEvent ));
  154.  
  155.     QuitApp ();        // pass it on to the app specific routine
  156.  
  157.     if ( reply->dataHandle != NULL )    /*    a reply is sought */
  158.         FailErr(err = AEPutParamPtr ( reply, 'errs', 'TEXT', "Qutting", 7 ));
  159.     
  160.     return err;
  161. }
  162.  
  163.  
  164. /*    
  165.     This routine is the low level processing routine for both the 
  166.     odoc (Open Document) and pdoc (Print Document) events.
  167.     
  168.     This routine is the key one, since this is how we get the list of
  169.     files/folders/disks to process.  The first thing to do is the get the
  170.     list of files, and then make sure that's all the parameters (should be!).
  171.     We then send call the PreflightDocs routine (from DSUserProcs), process
  172.     each file in the list by calling OpenDoc (again in DSUserProcs), and finally
  173.     call PostflightDocs (you know where) and setting a return value.
  174. */
  175. #pragma segment AEVT
  176. pascal OSErr _HandleDocs ( AppleEvent *theAppleEvent, AppleEvent *reply, Boolean opening ) {
  177. #pragma unused ( reply )
  178. #pragma unused ( handlerRefcon )
  179.     OSErr        err;
  180.     FSSpec        myFSS;
  181.     AEDescList    docList;
  182.     long        index, itemsInList;
  183.     Size        actualSize;
  184.     AEKeyword    keywd;
  185.     DescType    typeCode;
  186.     Handle        userDataHandle;
  187.     
  188.  
  189.     FailErr(err = AEGetParamDesc ( theAppleEvent, keyDirectObject, typeAEList, &docList ));
  190.     FailErr(err = GotRequiredParams ( theAppleEvent ));
  191.  
  192.     if (PreFlightDocs (opening, &userDataHandle))    {    // let the app do any preflighting it might need
  193.  
  194.         /*    How many items do we have?. */
  195.         FailErr(err = AECountItems ( &docList, &itemsInList ));
  196.     
  197.         for ( index = 1; index <= itemsInList; index++ ) {
  198.             FailErr(err = AEGetNthPtr ( &docList, index, typeFSS, &keywd, &typeCode,
  199.                     (Ptr) &myFSS, sizeof ( myFSS ), &actualSize ));
  200.     
  201.             OpenDoc( &myFSS, opening, userDataHandle );    // call the userProc
  202.         }
  203.     
  204.         PostFlightDocs (opening, userDataHandle);    // cleanup time
  205.     }
  206.     else
  207.         err = errAEEventNotHandled;    // tells AEM that we didn't handle it!
  208.         
  209.     FailErr(AEDisposeDesc ( &docList ));
  210.  
  211.     return err;
  212. }
  213.  
  214. /*
  215.     This routine is the handler for the odoc (Open Document) event.
  216.     
  217.     The odoc event simply calls the common _HandleDocs routines, which will
  218.     do the dirty work of parsing the AEVT & calling the userProcs.
  219. */
  220. #pragma segment AEVT
  221. pascal OSErr HandleODOC ( AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefcon ) {
  222. #pragma unused ( handlerRefcon )
  223.     
  224.     return (_HandleDocs(theAppleEvent, reply, true));    // call the low level routine
  225. }
  226.  
  227. /*
  228.     This routine is the handler for the pdoc (Print Document) event.
  229.     
  230.     The pdoc event like the odoc simply calls the common _HandleDocs routines
  231. */
  232. #pragma segment AEVT
  233. pascal OSErr HandlePDOC ( AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefcon ) {
  234. #pragma unused ( handlerRefcon )
  235.     
  236.     return (_HandleDocs(theAppleEvent, reply, false));    // call the low level routine
  237. }
  238.  
  239. /*    
  240.     This is the routine called by the main event loop, when a high level
  241.     event is found.  Since we only deal with Apple events, and not other
  242.     high level events, we just pass everything onto the AEM via AEProcessAppleEvent
  243. */
  244. #pragma segment AEVT
  245. pascal void DoHighLevelEvent ( EventRecord *event ) {
  246.  
  247.     FailErr ( AEProcessAppleEvent ( event ) );
  248. }
  249.